#pragma once
#include <vector>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

class statistics
{
private:
	struct data
	{
		long long sum;
		int n;
		data()
		{
			n = sum = 0;
		}
	};

	vector<data> v;
public:

	statistics()
	{
	}

	~statistics()
	{
	}

	void clear()
	{
		v.clear();
		for (int i = 0; i < 1000; i++)
		{
			v.push_back(data());
		}
	}

	void read_from_file(string filename)
	{
		clear();
		try
		{
			ifstream fin(filename);
			while (!fin.eof())
			{
				long long sal;
				fin >> sal;
				if (sal < 1000000)
				{
					v[sal / 1000].n++;
					v[sal / 1000].sum += sal;
				}
			}
			fin.close();
		}
		catch (exception e)
		{
			cout << e.what() << endl;
		}
	}

	int calculate_mean()
	{
		long long sum = 0;
		int n = 0;
		for (auto &d : v)
		{
			sum += d.sum;
			n += d.n;
		}
		return sum / n;
	}

	int percentile(int procent)
	{
		int n = 0;
		for (auto &d : v)
		{
			n += d.n;
		}
		n *= (procent / 100.0);
		int i = 0;
		while (n > 0)
		{
			n -= v[i].n;
			i++;
		}
		return v[i].sum / v[i].n;
	}
};

